<template>
  <view>
    <!-- 收获地址按钮 -->
    <view v-if="JSON.stringify(addressInfo) === '{}'" class="address-button">
      <button type="primary" size="mini" @click="chooseAddress">请选择收货地址+</button>
    </view>

    <!-- 收获地址详细信息 -->
    <view v-else class="address-detail" @click="chooseAddress">
      <!-- 收货人 电话 -->
      <view class="receive-person_phone">
        <view>收货人: <text>{{addressInfo.userName}}</text></view>
        <view class="phone-icon">
          <view>电话: <text>{{addressInfo.telNumber}}</text></view>
          <uni-icons type="forward" size="18"></uni-icons>
        </view>
      </view>
      <!-- 收货地址 -->
      <view class="receive-address">
        <view class="address-title">收货地址: </view>
        <view>{{addressConnect}}</view>
      </view>
    </view>

    <!-- 图片 -->
    <image class="cart-border" src="../../static/cart_border@2x.png"></image>
  </view>
</template>

<script>
  import {
    mapState,
    mapMutations,
    mapGetters
  } from 'vuex'
  export default {
    name: "my-address",
    data() {
      return {

      };
    },

    computed: {
      ...mapState('m_user', ['addressInfo']),
      ...mapGetters('m_user', ['addressConnect']),
    },

    methods: {
      ...mapMutations('m_user', ['updateAddress']),
      // 选择收货地址
      async chooseAddress() {
        /**
         * 1. 调用小程序提供的 chooseAddress() 方法，即可使用选择收货地址的功能
         * 返回值是一个数组：
         *   err-错误对象；
         *   success-为成功之后的收货地址对象
         */
        const [err, success] = await uni.chooseAddress().catch(err => err)
        // console.log(err)
        // console.log(success)
        // 用户成功的选择了收货地址
        if (err === null && success.errMsg === "chooseAddress:ok") {
          // 为 data 里面的收货地址对象赋值
          // this.addressInfo = success
          this.updateAddress(success)
        }
        // 用户没有授权
         if (err && (err.errMsg === 'chooseAddress:fail auth deny' || err.errMsg === 'chooseAddress:fail authorize no response')) {
           // 调用 this.reAuth() 方法，向用户重新发起授权申请
            this.reAuth()
          }
      },

      // 调用此方法，重新发起收货地址的授权
      async reAuth() {
        // 提示用户对地址进行授权
        const [err2, confirmResult] = await uni.showModal({
          content: '检测到您没打开地址权限，是否去设置打开？',
          confirmText: "确认",
          cancelText: "取消",
        })

        // 如果弹框异常，则直接退出
        if (err2) return

        // 如果用户点击了 “取消” 按钮，则提示用户 “您取消了地址授权！”
        if (confirmResult.cancel) return uni.$showMsg('您取消了地址授权！')

        // 如果用户点击了 “确认” 按钮，则调用 uni.openSetting() 方法进入授权页面，让用户重新进行授权
        if (confirmResult.confirm) return uni.openSetting({
          // 授权结束，需要对授权的结果做进一步判断
          success: (settingResult) => {
            // 地址授权的值等于 true，提示用户 “授权成功”
            if (settingResult.authSetting['scope.address']) return uni.$showMsg('授权成功！请选择地址')
            // 地址授权的值等于 false，提示用户 “您取消了地址授权”
            if (!settingResult.authSetting['scope.address']) return uni.$showMsg('您取消了地址授权！')
          }
        })
      }
    }
  }
</script>

<style lang="scss">
  .address-button {
    display: flex;
    height: 90px;
    justify-content: center;
    align-items: center;
    margin: 10px 0;
    font-size: 14px;
  }

  .address-detail {
    display: flex;
    font-size: 12px;
    height: 90px;
    flex-direction: column;
    justify-content: center;
    padding: 0 5px;

    .receive-person_phone {
      display: flex;
      justify-content: space-between;

      .phone-icon {
        display: flex;
        align-items: center;
      }
    }

    .receive-address {
      display: flex;
      align-items: center;
      justify-content: flex-start;
      margin-top: 10px;

      .address-title {
        white-space: nowrap;
        margin-right: 5px;
      }
    }
  }

  .cart-border {
    display: block;
    width: 100%;
    height: 5px;
    margin-top: 8px;
  }
</style>
